//
//  TCSGGeneralMethods.m
//  iPing
//
//  Created by Thomas on 8/10/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "TCSGGeneralMethods.h"


@implementation TCSGGeneralMethods

+(void)setLabelField:(NSString *)text
{
	NSLog(@"posting:%@",text);
	[[NSNotificationCenter defaultCenter] postNotificationName:TCSGLabelNot object:text];
}
+(NSString *)plistPathForHTMLCode:(NSString *)HTMLCode
{
	return [[TCSGGeneralMethods folderForHTMLCode:HTMLCode createDir:YES] stringByAppendingPathComponent:[HTMLCode stringByAppendingPathExtension:@"plist"]];
}
+(NSString *)epubPathForHTMLCode:(NSString *)HTMLCode
{
	NSLog(@"HTMLCode:%@",HTMLCode);
	NSLog(@"path for HTMLCode: %@",[TCSGGeneralMethods folderForHTMLCode:HTMLCode createDir:YES]);
	return [[TCSGGeneralMethods folderForHTMLCode:HTMLCode createDir:YES] stringByAppendingPathComponent:[HTMLCode stringByAppendingPathExtension:@"epub"]];
}
+(NSString *)fullStoryPathForHTMLCode:(NSString *)HTMLCode
{
	return [[TCSGGeneralMethods folderForHTMLCode:HTMLCode createDir:YES] stringByAppendingPathComponent:[HTMLCode stringByAppendingPathExtension:@"html"]];
}
+(NSString *)folderForHTMLCode:(NSString *)HTMLCode createDir:(BOOL)create
{
	NSString *path = [[TCSGGeneralMethods storiesFolder] stringByAppendingPathComponent:HTMLCode];
	BOOL isDir;
	if([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir] && create)
	{
		if(!isDir)
		{
			[[NSFileManager defaultManager] movePath:path toPath:[path stringByAppendingPathExtension:@"old"] handler:nil];
			[[NSFileManager defaultManager] createDirectoryAtPath:path attributes:nil];
		}
	}
	else if (create)
	{
		[[NSFileManager defaultManager] createDirectoryAtPath:path attributes:nil];
	}
	return [path autorelease];
}
+(NSDictionary *)infoDict:(NSString *)path
{
	NSTask *task = [[NSTask alloc] init];
    [task setLaunchPath:@"/usr/sbin/diskutil"];
    
    NSArray *args = [NSArray arrayWithObjects:@"info", path, nil];
    [task setArguments:args];
	
    NSPipe *outPipe = [[NSPipe alloc] init];
    [task setStandardOutput:outPipe];
    [outPipe release];
    
    [task launch];
    
    // Read the output
    NSData *thedata = [[outPipe fileHandleForReading] readDataToEndOfFile];
    [task waitUntilExit];
    [task release];

    // Convert to a string
    NSString *aString = [[NSString alloc] initWithData:thedata
                                              encoding:NSUTF8StringEncoding];
	
    // Break the string into lines
	NSArray * filenames = [[aString componentsSeparatedByString:@"\n"] retain];
	NSMutableDictionary *dataDict = [[NSMutableDictionary alloc] init];
	int i;
	for (i=0;i<[filenames count];i++)
	{
		NSArray *tempArray = [[filenames objectAtIndex:i] componentsSeparatedByString:@": "];
		if([tempArray count]>=2)
		{
			NSString *tempArray2 = [[tempArray objectAtIndex:0] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
			NSString *tempArray3 = [[tempArray objectAtIndex:1] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
			[dataDict setObject:tempArray3 forKey:tempArray2];
		}
		
	}
    
    // Release the string
    [aString release];
	return [dataDict autorelease];
	
}
+(void)checkBundleExistance
{

	BOOL isDir = NO;
	NSLog(@"%@",[NSString stringWithFormat:@"%@/Library/Application Support/%@/Stories/",NSHomeDirectory(),SGNAME,nil]);
	if(![[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/Library/Application Support/%@/Stories/",NSHomeDirectory(),SGNAME,nil] isDirectory:&isDir] || !isDir)
	{
		[[NSFileManager defaultManager] createDirectoryAtPath:[NSString stringWithFormat:@"%@/Library/Application Support/%@/Stories/",NSHomeDirectory(),SGNAME,nil] withIntermediateDirectories:YES attributes:nil error:nil];
	}
	if(![[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/Library/Application Support/%@/tmp/",NSHomeDirectory(),SGNAME,nil] isDirectory:&isDir] || !isDir)
	{
		[[NSFileManager defaultManager] createDirectoryAtPath:[NSString stringWithFormat:@"%@/Library/Application Support/%@/tmp/",NSHomeDirectory(),SGNAME,nil] withIntermediateDirectories:YES attributes:nil error:nil];
	}
}
+(NSString *)storiesFolder
{
	return [[TCSGGeneralMethods dbFolder] stringByAppendingPathComponent:@"Stories"];
}
+(NSString *)tempFolder
{
	return [[TCSGGeneralMethods dbFolder] stringByAppendingPathComponent:@"tmp"];
}
+(NSString *)dbFolder
{

	return [NSString stringWithFormat:@"%@/Library/Application Support/%@/",NSHomeDirectory(),SGNAME,nil];
}
+(NSString *)dbPath
{
	return [[TCSGGeneralMethods dbFolder] stringByAppendingPathComponent:@"library.sgdb"];
}
+(NSDictionary *)getDB
{
	NSDictionary *db;
	if([[NSFileManager defaultManager] fileExistsAtPath:[TCSGGeneralMethods dbPath]])
	{
		NSLog(@"db file exists");
		db = [NSDictionary dictionaryWithContentsOfFile:[TCSGGeneralMethods dbPath]];
	}
	else
	{
		db = [NSDictionary dictionaryWithObjectsAndKeys:nil];
	}
	return db;
}
+(void)writeDB:(NSDictionary *)DB
{
	[DB writeToFile:[TCSGGeneralMethods dbPath] atomically:YES];
}
+(BOOL)SaveIndividualChapters
{
	return YES;
}
@end
